package com.atguigu.springcloud.service.Impl;

import com.atguigu.springcloud.dao.OrderDao;
import com.atguigu.springcloud.domain.Order;
import com.atguigu.springcloud.service.AccountService;
import com.atguigu.springcloud.service.OrderService;
import com.atguigu.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private StorageService storageService;
    @Autowired
    private AccountService accountService;
    @Override
    @GlobalTransactional(name="ads-asa-asd",rollbackFor = Exception.class)
    public void create(Order order) {
       log.info("----创建订单");
       orderDao.create(order);

       log.info("---->订单调用库存扣减");
       storageService.decrease(order.getProductId(),order.getCount());
       log.info("---->库存扣减成功");

       log.info("--->>账户-->扣减");
       accountService.decrease(order.getUserId(),order.getMoney());
       log.info("--->>账户-->扣减成功");

//       修改订单状态
        log.info("修改订单状态");
        orderDao.update(order.getUserId(),0);
        log.info("--->修改订单状态结束end");
    }

}
